﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

using UncleBobWebService.Models;
using UncleBobWebService.ServiceRequester;

namespace UncleBobWebService.Database
{
    class UserAnswersDBAdapater : IDBAdapter
    {
        public override ServiceRequester.Message CreateData(ServiceRequester.Message msg, object data)
        {
            msg.Result = true;
            JobApplication jobApp = msg.Data as JobApplication;

            using (SqlConnection connection = new SqlConnection(mConnectionString))
            using (SqlCommand command = connection.CreateCommand())
            {
                foreach (ApplicationQuestion aQs in jobApp.Job.QuestionsList.Questions)
                {                    
                    if (aQs.GetType() == typeof(MultipleChoiceQuestion))
                    {
                        Answer correctAnswer = new Answer();
                        MultipleChoiceQuestion mQ = (MultipleChoiceQuestion)aQs;                        
                        foreach (Answer a in mQ.PossibleAnswers.Values)
                        {
                            if (a.IsCorrect)
                                correctAnswer = a;
                        }
                        command.CommandText = string.Format("INSERT INTO User_Job_Questions (JobApplicationID, Question, Answer,Reviewed, Accepted) VALUES ({0}, '{1}', '{2}', '{3}', '{4}')"
                            , jobApp.AppID, mQ.Question, correctAnswer.TheAnswer, "False", correctAnswer.IsCorrect);
                    }
                    else
                    {
                        ExtendedQuestion eQ = (ExtendedQuestion)aQs;
                        command.CommandText = string.Format("INSERT INTO User_Job_Questions (JobApplicationID, Question, Answer,Reviewed, Accepted) VALUES ({0}, '{1}', '{2}', '{3}', '{4}')"
                           , jobApp.AppID, eQ.Question, eQ.Response, false, false);                       
                    }

                    try
                    {
                        connection.Open();
                        command.ExecuteNonQuery();                                              
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        msg.Result = false;                        
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }

            return msg;
        }

        public override ServiceRequester.Message SearchForSpecificData(ServiceRequester.Message msg, object data)
        {
            msg.Result = true;
            JobApplication jobApp = msg.Data as JobApplication;            

            using (SqlConnection connection = new SqlConnection(mConnectionString))
            using (SqlCommand command = connection.CreateCommand())
            {
                DataTable dt = new DataTable();

                command.CommandText = string.Format("SELECT * FROM User_Job_Questions WHERE JobApplicationID = {0}", jobApp.AppID);

                try
                {
                    SqlDataAdapter SqlAdapter = new SqlDataAdapter(command);
                    connection.Open();
                    SqlAdapter.Fill(dt);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    msg.Result = false;
                    return msg;
                }

                finally
                {
                    connection.Close();
                }

                foreach (DataRow dr in dt.Rows)
                {
                    ExtendedQuestion eX = new ExtendedQuestion();
                    eX.Accepted = Convert.ToBoolean(dr["Accepted"]);
                    eX.Question = Convert.ToString(dr["Question"]);
                    eX.Response = Convert.ToString(dr["Answer"]);
                    eX.Reviewed = Convert.ToBoolean(dr["Reviewed"]);

                    jobApp.Job.QuestionsList.AddQuestion(eX);
                }
            }

            jobApp.Job.QuestionsList.UserAnswers = true;
            msg.Data = jobApp;
            return msg;
        }

        public override ServiceRequester.Message SearchForAllData(ServiceRequester.Message msg)
        {
            //msg.Result = true;
            //List<JobApplication> jobApps = msg.Data as List<JobApplication>;
            //foreach (JobApplication job in jobApps)
            //{
            //    Message tempMessage = new Message(MessageTypes.
            //    tempMessage.Data = job;
            //    tempMessage = SearchForSpecificData(tempMessage, tempMessage.Data);
            //    if (tempMessage.Result == false)
            //    {
            //        msg.Result = false;
            //        break;
            //    }
            //}

            //return msg;

            throw new NotImplementedException();
        }

        public override ServiceRequester.Message EditSpecificData(ServiceRequester.Message msg, object data)
        {
            throw new NotImplementedException();
        }

        public override ServiceRequester.Message DeleteSpecificData(ServiceRequester.Message msg, object data)
        {
            throw new NotImplementedException();
        }

        public override ServiceRequester.Message DeleteAllData(ServiceRequester.Message msg)
        {
            throw new NotImplementedException();
        }
    }
}
